package com.hospital.billcommon.module.filter;

import com.hospital.billcommon.module.entity.ChargeSpecialExcept;
import com.hospital.billcommon.module.entity.ChargeSpecialItem;
import com.hospital.common.core.module.entity.ChargeSpecialExceptDict;
import com.hospital.common.core.module.entity.ChargeSpecialItemDict;
import com.hospital.common.core.module.entity.query.BillItem;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description 排斥项实现类
 * @author: liuwei
 * @create 2020/5/14 10:15 上午
 **/

@CutOffFilter
public class ExcludeCutOff implements CutOff {
    @Override
    public void executeCutOff(List<? extends BillItem> billItemList, String chargeType, String outpOrInp, List<ChargeSpecialItemDict> chargeSpecialItemDicts , List<ChargeSpecialExceptDict> chargeSpecialExceptDicts) {
        List<ChargeSpecialExceptDict> chargeSpecialExceptList =chargeSpecialExceptDicts;
        List<String> itemClassList = billItemList.stream().map(BillItem::getItemClass).distinct().collect(Collectors.toList());
        List<ChargeSpecialExceptDict> orderCharges = chargeSpecialExceptList.stream().filter(c -> c.getChargeType().equals(chargeType) && itemClassList.contains(c.getItemClass()) && ((String.valueOf(Integer.parseInt(outpOrInp)).equals(c.getFlag()) || "3".equals(c.getFlag())))).collect(Collectors.toList());

        for (BillItem item : billItemList) {
            if (!item.isCount()) {
                ChargeSpecialExceptDict chargeSpecialExcept = orderCharges.stream().filter(c ->
                        c.getItemClass().equals(item.getItemClass())
                                && ("*".equals(c.getItemCode()) || c.getItemCode().equals(item.getItemCode()))
                                && ("*".equals(c.getItemSpec()) || c.getItemSpec().equals(item.getItemSpec()))
                ).findFirst().orElse(null);
                if (chargeSpecialExcept != null) {
                    item.setCharges(Math.round(item.getCosts() * item.getAmount() * ((double) chargeSpecialExcept.getProportionNumerator() / chargeSpecialExcept.getProportionDenominator()) * 100) * 0.01d);
                    item.setCosts(Math.round(item.getCosts() * item.getAmount() * 100) * 0.01d);
                    if (item.getChargePriceIndicator()==1) {
                        item.setCurrentPrice(Math.round(item.getCosts() * ((double) chargeSpecialExcept.getProportionNumerator() / chargeSpecialExcept.getProportionDenominator()) * 1000) * 0.001d);
                    }
                    item.setCount(true);
                }
            }
        }
    }
}